﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Infrastructure;

namespace Picking_Winform
{
    public partial class SetupEnvironmentView : FormBase
    {
        public SetupEnvironmentView()
        {
            InitializeComponent();
            txtTitle.Text = "环境设定";
            this.Controls.Add(txtTitle);
        }

        protected override int GetFormWidth()
        {
            return 700;
        }

        /// <summary>
        /// Init AIPlug
        /// </summary>
        private void InitAIPlug()
        {
            if (!string.IsNullOrWhiteSpace(ConfigInfo.AxAiPlugIpAddress))
            {
                axAiPlug.ConnectSocket(ConfigInfo.AxAiPlugIpAddress, ConfigInfo.AxAiPlugPort);

                axAiPlug.StartPolling(); //watch the data arrival

                axAiPlug.BinaryMode = true; //set to true

                axAiPlug.CommandTimeout = 5000; //set to 5000
            }
        }

        /// <summary>
        /// Turn On LED Yellow
        /// </summary>
        private void TurnOnLEDYellow()
        {
            //this.axAiPlug.SendCommand("T" + ConfigInfo.CmdLEDYellow.Trim());
        }

        /// <summary>
        /// Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SetupEnvironmentView_Load(object sender, EventArgs e)
        {
            this.InitAIPlug();
        }

        /// <summary>
        /// OK
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            this.lblInfo.Text = string.Empty;

            string currentTime = int.Parse(DateTime.Now.ToString("HHmm")).ToString("0000");

            if (this.txtPassword.Text.Trim().Length == 4)
            {
                string password = string.Empty;
                password += this.txtPassword.Text.Trim().Substring(3, 1);
                password += this.txtPassword.Text.Trim().Substring(2, 1);
                password += this.txtPassword.Text.Trim().Substring(1, 1);
                password += this.txtPassword.Text.Trim().Substring(0, 1);

                if (string.Equals(currentTime, password))
                {
                    this.pnlPassword.Visible = false;
                    this.pnlConfig.Location = new Point(72, 150);
                    this.pnlConfig.Visible = true;
                    return;
                }
            }

            this.lblInfo.Text = "密码不正确!";
        }


        private void btnConfigSetup_Click(object sender, EventArgs e)
        {
            this.TurnOnLEDYellow();
            if (!this.btnConfigSetup.Visible)
            {
                return;
            }

            FormBase form = new ConfigSetupView();
            form.ShowDialog(this);
        }

        private void btnWorkersAndLightsSetup_Click(object sender, EventArgs e)
        {
            this.TurnOnLEDYellow();
            if (!this.btnWorkersAndLightsSetup.Visible)
            {
                return;
            }

            FormBase form = new WorkersAndLightsSetupView();
            form.ShowDialog(this);
        }

        private void btnSebangoAndDNoSetup_Click(object sender, EventArgs e)
        {
            this.TurnOnLEDYellow();
            if (!this.btnSebangoAndDNoSetup.Visible)
            {
                return;
            }

            FormBase form = new SebangoAndDNoSetupView();
            form.ShowDialog(this);
        }

        private void btnLightsAndDNoSetup_Click(object sender, EventArgs e)
        {
            this.TurnOnLEDYellow();
            if (!this.btnLightsAndDNoSetup.Visible)
            {
                return;
            }

            FormBase form = new LightsAndDNoSetupView();
            form.ShowDialog(this);
        }

        private void btnReturn_Click(object sender, EventArgs e)
        {
            //this.axAiPlug.SendCommand("Z");
            this.CloseCurrentWindow();
        }
    }
}
